package com.zw.pdm.module.report.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.rep.Change;
import com.zw.pdm.entity.rep.Plan;
import com.zw.pdm.entity.rep.Result;
import com.zw.pdm.entity.rep.Sheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.report.service.ReportQueryService;
import com.zw.pdm.module.report.service.SheetService;
import com.zw.pdm.module.system.service.DeptService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.*;

/**
 * @Description: 计划查询接口
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/report/query")
@SuppressWarnings("all")
public class ReportQueryController extends BaseController {

    @Autowired
    private ReportQueryService reportQueryService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;
    @Autowired
    private DeptService deptService;

    @GetMapping
    @RequiresPermissions(value = {"report:query:view", "dsn:prodetQuer:view"}, logical = Logical.OR)
    public String page(String proCode, Integer reportType, Model model) {
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_MALTYPE));
        model.addAttribute("depts", deptService.findAllTopLevelDept());
        model.addAttribute("proCode", proCode);
        model.addAttribute("reportType", reportType);
        return "report/query/page";
    }

    /**
     * 计划列表
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions(value = {"report:query:view", "dsn:prodetQuer:view"}, logical = Logical.OR)
    public JsonResult list(Sheet sheet) {
        startPage();
        List<Sheet> list = reportQueryService.findWithReportQuery(sheet);
        return jsonResult(list);
    }

    /**
     * 计划单查看详细计划页面
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions(value = {"report:query:view", "dsn:prodetQuer:view"}, logical = Logical.OR)
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        Sheet sheet = sheetService.getSheetById(sheetId);
        model.addAttribute("process", sheetService.findVpBySheetId(sheetId));
        model.addAttribute("sheet", sheet);
        if (sheet.getSheetType() == 1) {
            model.addAttribute("plans", sheetService.findPlanBySheetId(sheetId));
            return "report/query/info";
        } else {
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", sheetService.findChangeBySheetId(sheetId));
            return "report/query/changeInfo";
        }
    }

    /**
     * 计划列表
     */
    @ResponseBody
    @PostMapping("/plan/list")
    @RequiresPermissions({"report:query:view"})
    public JsonResult planList(Plan plan) {
        startPage();
        List<Plan> list = reportQueryService.findPlanDetailList(plan);
        return jsonResult(list);
    }

    /**
     * 变更列表
     */
    @ResponseBody
    @PostMapping("/change/list")
    @RequiresPermissions({"report:query:view"})
    public JsonResult planList(Change change) {
        startPage();
        List<Change> list = reportQueryService.findChangeDetailList(change);
        return jsonResult(list);
    }

    /**
     * 查看打印
     */
    @GetMapping("/print/{id}")
    @RequiresPermissions({"report:query:view"})
    public String print(@PathVariable("id") Integer sheetId, Model model) {
        Sheet sheet = sheetService.getSheetById(sheetId);
        model.addAttribute("process", sheetService.findVpBySheetId(sheetId));
        model.addAttribute("sheet", sheet);
        if (sheet.getSheetType() == 1) {
            model.addAttribute("plans", sheetService.findPlanBySheetId(sheetId));
            return "report/list/print";
        } else {
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", sheetService.findChangeBySheetId(sheetId));
            return "report/list/changePrint";
        }
    }

    @GetMapping("/detailList")
    @RequiresPermissions({"dsn:queryDetail:view"})
    public String outPage(Model model) {
        model.addAttribute("branchName", "生产调度科");
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "report/query/pageDetail";
    }

    /**
     * 提报计划明细查询(默认查询生成调度科)
     *
     * @param result
     * @return
     */
    @ResponseBody
    @PostMapping("/detailList")
    @RequiresPermissions({"dsn:queryDetail:view"})
    public JsonResult queryDetail(Result result) {
        startPage();
        List<Result> list = reportQueryService.findRepResultByQuery(result);
        return jsonResult(list);
    }



}
